package com.hy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.domain.Blog;
import com.hy.domain.Tag;
import com.hy.domain.User;
import com.hy.domain.dto.BlogDTO;
import com.hy.domain.dto.PageQuery;
import com.hy.domain.dto.R;
import com.hy.domain.dto.UserDTO;
import com.hy.exception.BusinessException;
import com.hy.service.BlogService;
import com.hy.mapper.BlogMapper;
import com.hy.service.TagService;
import com.hy.service.UserService;
import com.hy.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author hou
* @description 针对表【blog(文章信息)】的数据库操作Service实现
* @createDate 2023-04-23 19:04:41
*/
@Service
@Transactional
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
    implements BlogService{

    @Autowired
    private TagService tagService;

    @Autowired
    private UserService userService;

    @Override
    public List<Blog> listBySectionId(int id) {
        return lambdaQuery().eq(Blog::getSectionId, id).list();
    }

    @Override
    public Page listPagesByQuery(int pageNum, int size, String keyword) {
        if (pageNum < 1) {
            pageNum = 1;
        }

        if (size < 1) {
            size = 10;
        }
//        int start = pageNum * size;
//        final List<BlogDTO> list = this.baseMapper.listBlogAndUserInfoByLimit(start, size, keyword);
        final Page<Blog> page = new Page<>();
        QueryWrapper qw = new QueryWrapper();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("title", keyword);
        }

        final Page page1 = page(page, qw);
        return page(page, qw);
    }

    /**
     * 根据用户id查询文章
     * @param id 用户id
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public IPage listPagesByUser(Long id, int pageNum, int size) {
        lambdaQuery().eq(Blog::getUserId, id);
        IPage page = new Page<>(pageNum, size);
        final LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<Blog>().eq(Blog::getUserId, id);
        getBaseMapper().selectPage(page, wrapper);
        return page;
    }

    @Override
    public BlogDTO getBlogById(long id) {
        final Blog blog = getById(id);
        // 查询用户信息
        final User user = userService.getById(blog.getUserId());
        // 脱敏
        final BlogDTO result = BeanUtil.copyProperties(blog, BlogDTO.class);
        result.setUser(BeanUtil.copyProperties(user, UserDTO.class));
        return result;
    }

//    @Override
//    public R<Blog> saveBlog(Blog blog) {
////        final Blog blog = dto.getBlog();
////        final List<Tag> tags = dto.getTags();
//        save(blog);
////        tagService.saveBatch(tags);
//        return R.ok();
//    }

    @Override
    public R updateBlog(Blog blog) {
//        final Blog blog = dto.getBlog();
        this.updateById(blog);
        final List<Long> tagIds = tagService.lambdaQuery().eq(Tag::getBlogId, blog.getId())
                .list()
                .stream()
                .map(Tag::getTagId)
                .collect(Collectors.toList());
//        tagService.removeByIds(tagIds);
        // TODO 更新tag表
        return R.ok();
    }

    @Override
    public void saveBlog(Blog blog) {
        final UserDTO user = UserHolder.getUser();
        blog.setCreateTime(LocalDateTime.now());
        blog.setUpdateTime(LocalDateTime.now());
        blog.setUserId(user.getUserId());
        blog.setLikeCount(0);
        blog.setCommentCount(0);
        save(blog);
    }
}




